<template>
  <!-- 上传厂家证照的表格modal -->
  <div>
    <basic-modal
      :modalOpt="modal"
      :buttonText="['整单收货','关闭']"
      @onMakeSure="receiveAll"
      @closeModal="closeModal"
      :footer="footer"
    >
      <template #cusBtn>
      </template>
      <template>
        <batch-operat-btn
          v-if="editable"
          :isView="false"
          :optData="optText"
          @onOptData="onBatchAudit"
        >
          <template #cusBtn> </template>
        </batch-operat-btn>
        <!-- <a-list-info :infoDatas="infoDatas" /> -->
        <div class="recordBox">
          <a-list-table
            :columns="columns"
            :tableData="listdata"
            :isSelecttion="false"
            :loading="loading"
            :scroll="{ y: null }"
            :pagination="pagination"
            @onTableChange="handleTableChange"
            @onAction="onAction"
          >
          </a-list-table>
        </div>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '@/views/app/biz/common/basicModal'
import aListTable from '@/views/app/biz/common/aListTable'
import aListInfo from '@/views/app/biz/common/aListInfo'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'

import {
  getInfoByPid,
  getDetailByConditions,
  getDetailByConditionsCount,
  receiveOrder,
  receiveGoods
} from '../saleDelivery/saleDelivery'

export default {
  components: {
    basicModal,
    aListTable,
    batchOperatBtn,
    aListInfo
  },
  data () {
    return {
      footer: undefined,
      editable: false,
      loading: false,
      optText: '批量收货',
      modal: {
        modalVisible: false,
        modalTitle: '确认收货',
        modalWidth: '80vw',
        maxHeight: '70vh'
      },
      infoDatas: [
        { title: '采购单位名称', id: 'customerName', value: '' },
        { title: '销售单位名称', id: 'supplierName', value: '' },
        { title: '采购单号', id: 'purOrderNo', value: '' },
        { title: '采购单名称', id: 'purOrderName', value: '' },
        { title: '配送单号', id: 'deliveryOrderNo', value: '' },
        { title: '配送单名称', id: 'deliveryOrderName', value: '' },
        { title: '是否关联多个订单', id: 'multipleOrder', value: '' },
        { title: '关联订单', id: 'associatedOrder', value: '' },
        { title: '送货类型', id: 'deliveryType', value: '' },
        { title: '寄件人姓名', id: 'sender', value: '' },
        { title: '寄件人联系电话', id: 'senderTel', value: '' },
        { title: '寄件人证件类型', id: 'senderCertificateType', value: '' },
        { title: '寄件人证件号码', id: 'senderCertificateNo', value: '' },
        { title: '收件人姓名', id: 'receiver', value: '' },
        { title: '收件人联系电话', id: 'receiverTel', value: '' },
        { title: '收件地址', id: 'receiverAddr', value: '' },
        { title: '状态', id: 'deliveryState', value: '' },
        { title: '物流关联单号', id: 'logisticsNo', value: '' },
        { title: '物流方', id: 'logistics', value: '' },
        { title: '配送单生成时间', id: 'deliveryCreateTime', value: '' },
        { title: '拣配完成时间', id: 'finshPickTime', value: '' },
        { title: '发货时间', id: 'deliveryTime', value: '' },
        { title: '到货时间', id: 'arrivalTime', value: '' },
        { title: '收货时间', id: 'receiptTime', value: '' }
      ],
      columns: [
        { title: '销售单位名称', dataIndex: 'supplierName', key: 'supplierName', align: 'center' },
        { title: '采购单号', dataIndex: 'purOrderNo', key: 'purOrderNo', align: 'center' },
        { title: '配送单号', dataIndex: 'deliveryOrderNo', key: 'deliveryOrderNo', align: 'center' },
        { title: '商品编号', dataIndex: 'saleGoodsCode', key: 'saleGoodsCode', align: 'center' },
        { title: '品名', dataIndex: 'productNameZh', key: 'productNameZh', align: 'center' },
        { title: '通用名', dataIndex: 'commonName', key: 'commonName', align: 'center' },
        // { title: '型号', dataIndex: 'model', key: 'model', align: 'center' },
        { title: '规格', dataIndex: 'spec', key: 'spec', align: 'center' },
        { title: '生产厂家名称', dataIndex: 'manufacturerZh', key: 'manufacturerZh', align: 'center' },
        { title: '批号', dataIndex: 'batchCode', key: 'batchCode', align: 'center' },
        // { title: '效期', dataIndex: 'expiryDate', key: 'expiryDate', align: 'center' },
        // { title: '计量单位', dataIndex: 'measurement', key: 'measurement', align: 'center' },
        { title: '采购数量', dataIndex: 'num', key: 'num', align: 'center' },
        { title: '单品含税价', dataIndex: 'iunitprice', key: 'iunitprice', align: 'center', scopedSlots: { customRender: 'tofixed2' } },
        { title: '含税总价', dataIndex: 'totalPrice', key: 'totalPrice', align: 'center', scopedSlots: { customRender: 'tofixed2' } },
        { title: '状态', dataIndex: 'deliveryState', key: 'deliveryState', align: 'center' },
        {
          title: '操作',
          dataIndex: 'action',
          key: 'operation',
          width: 180,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      sData: {
        deliveryOrderPid: null
      },
      listdata: [],
      pagination: {
        //分页配置
        current: 1, //当前页码 current
        pageSize: 10, //每页显示条数 pageSize
        total: 1,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ['1', '5', '10', '100'],
      },
      listKey: 0,
      pid: '',
      zzPid: '',
      sysSeq: 0,
      loadings: false,
    }
  },
  computed: {},
  mounted () { },
  methods: {
    // // 弹出模态框
    showModal (val, record) {
      this.editable = true
      this.pid = val
      this.record = record
      this.modal.modalVisible = true
      this.modal.modalTitle = '确认收货'
      this.sData.deliveryOrderPid = val
      this.getSBypid(val)
      this.getTotal()
      this.getDetailBypid(this.sData)

    },
    // 关闭模态详细框
    closeModal () {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    closeModal2 () {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    //批量收货
    onBatchAudit () {

    },

    // 整单收货
    async receiveAll () {
      console.log(this.record);
      this.record.deliveryState = '已收货'
      let key = 'updatable'
      this.$message.loading({ content: '提交中', key, duration: 0 })
      let res = await receiveOrder(this.record)
      console.log(res)
      if (res && res.code && res.code === 'C1') {
        this.$message.success({ content: '提交成功', key, duration: 3 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败,请稍后再试', key, duration: 3 })
      }
    },

    // 得到采购订单消息
    async getSBypid (val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        this.sysSeq = res.data.sysSeq
        for (const key in res.data) {
          for (const item of this.infoDatas) {
            if (key == item.id) {
              item.value = res.data[key]
            }
          }
        }
      }
    },
    // 触发改变页码,筛选,排序时的回调
    handleTableChange (pagination, filters, sorter) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.getDetailBypid(this.sData)
    },
    // 初始化页面时得到总数
    async getTotal () {
      let pages = ''
      pages = await getDetailByConditionsCount(this.sData)
      console.log(pages)
      if (pages && pages.code && pages.code === 'C1') {
        const pagination = { ...this.pagination }
        pagination.total = pages.data
        this.pagination = pagination
        this.pagination.showTotal = (showTotal) => { return `共 ${showTotal} 条` }
      }
    },
    //得到初始化订单商品list
    async getDetailBypid (params) {
      console.log('请求列表时传入的数据', params)
      this.loading = true
      let listdata = []
      let res = ''
      res = await getDetailByConditions(params, this.pagination)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        const resdata = res.data
        this.orgList = res.data
        for (let item of resdata) {
          listdata.push({
            key: item.pid,
            action: ['收货'],
            ...item
          }
          )
        }
        this.listdata = listdata
      } else {
        this.listdata = []
      }
    },
    onAction (text, record, index) {
      if (text == '收货') {
        this.showConfirm(record, '1')
      }
    },
  },
}
</script>

<style>
.recordBox {
  width: 100%;
  min-height: 80px;
}
</style>